<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatusList">
        <el-option v-for="item in cardStatusList" 
        :key="item.id" 
        :value="item.id"
        :label="item.name" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="serch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button @click="$router.push('/cardAdd')" type="primary">添加月卡</el-button>
      <el-button @click="dels">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleCheckAllChange">
        <!--  多选框 -->
        <el-table-column type="selection" width="55" ></el-table-column>
        <el-table-column type="index" label="序号" />
   <!-- ! 列表数据 -->
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="phoneNumber" label="联系方式" />
        <el-table-column prop="carNumber" label="车牌号码" />
        <el-table-column prop="carBrand" label="车辆品牌" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
        <el-table-column prop="cardStatus" :formatter="formatStatus" label="状态" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/cardAdd?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
   <!-- 分页 -->
    <div class="page-container">
      <el-pagination
      @current-change='currentPage'
        layout="total, prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
      />
    </div>
  </div>
</template>

/* 
!查询 添加 编辑 删除 
 */

<script>
import { deliteCardAPI, getCardListAPI } from '@/api/card'
export default {
  data(){
    return {
      /* 列表数据 */
      params:{
        page:1,  //页数
        pageSize:10, // 条数
        carNumber:null,
        personName:null,
        cardStatus:null
      },
      list:[],
      total:0, // 总条数
       // 月卡状态
      cardStatusList: [
        {
          id: null,
          name: '全部'
        },
        {
          id: 0,
          name: '可用'
        },
        {
          id: 1,
          name: '已过期'
        }
      ],
      batches:[]
    }
  },
  created(){
    this.getList()
  },
  methods:{
    /* 搜索 */
    serch(){
      this.params.page = 1 
      this.getList()
    },
    /*  月卡 列表数据 */
   async getList(){
     const {data:{rows,total}} = await getCardListAPI(this.params)
     this.list = rows
     this.total = total
    },
    currentPage(val){   // val  可以拿到当前 点击的是第几页
      this.params.page = val
      this.getList()
    },
 /*
 ! 状态区分 */
     formatStatus(row){   ///  拿到当前行 的所有数据
 /*
 ! 第一种方法 */
     //  const MAP ={
      //   0:'可用',
      //   1:'已过期'
      //  }
       //  return MAP[row.cardStatus]
 /* 
 !   第二种方法 */
         return row.cardStatus === 0 ? '可用' :'已过期'
    },
    /* 删除 */
   async del(id){
      console.log(id);
      await deliteCardAPI(id)
      this.getList()
    },
    /*   拿到 选中的 id */
    handleCheckAllChange(selection){
      console.log(selection);
      this.batches = selection
    },
    /* 批量删除 */
   async dels(){
    if(this.batches.length === 0) {
      return this.$confirm('请勾选要删除的文件', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
    } 
    await deliteCardAPI(this.batches.map((item) => item.id).join(','))
      this.getList()
    },
  }
}
</script>


<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}


.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>